package com.soonfor.evaluate.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.estronger.shopmanage.R;
import com.soonfor.evaluate.activity.ShowPicActivity;
import com.soonfor.evaluate.adapter.EvaluateLevlShowAdapter;
import com.soonfor.evaluate.adapter.PhotoAdapter;
import com.soonfor.evaluate.bean.EvaluateListDetailBean;
import com.soonfor.evaluate.tools.FullyGridLayoutManager;

/**
 * 作者：DC-DingYG on 2018-10-17 13:57
 * 邮箱：dingyg012655@126.com
 */
public class EvaluateDetailView extends LinearLayout {
    private Context mContext;
    @BindView(R.id.tvfEvaluateTime)
    TextView tvfEvaluateTime;
    @BindView(R.id.llfAllEval)
    LinearLayout llfAllEval;
    @BindView(R.id.tvfOverall)
    TextView tvfOverall;
    @BindView(R.id.mRecyclerEvaluate)
    RecyclerView mRecyclerEvaluate;

    private EvaluateLevlShowAdapter levlAdapter;

    @BindView(R.id.llfEvaluateContent)
    LinearLayout llfEvaluateContent;
    @BindView(R.id.tvfEvaluateContent)
    TextView tvfEvaluateContent;
    @BindView(R.id.rvfEvalPics)
    RecyclerView rvfEvalPics;
    private GridLayoutManager pManager;
    private PhotoAdapter pAdapter;

    public EvaluateDetailView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        final View view = View.inflate(context, R.layout.view_evaluatetome_detail, this);
        ButterKnife.bind(this, view);
    }


    //初始化
    public void initEvaluateDetailView(final Activity mContext, final EvaluateListDetailBean detailBean) {
        this.mContext = mContext;
        //异步更新数据1
        new Thread(new Runnable() {
            @Override
            public void run() {
                mContext.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //是否开启整体评价
                            if(detailBean.getFifallhighappraise()==1){
                                llfAllEval.setVisibility(VISIBLE);
                                tvfOverall.setText(detailBean.getFallhighappraiseresultdesc());
                            }else {
                                llfAllEval.setVisibility(GONE);
                            }
                            tvfEvaluateTime.setText(detailBean.getFappraisedate());
                            //是否开启评价内容
                            if (detailBean.getFifsetappraisecontent() == 1) {
                                llfEvaluateContent.setVisibility(VISIBLE);
                                tvfEvaluateContent.setText(detailBean.getFappraisecontent());
                            } else {
                                llfEvaluateContent.setVisibility(GONE);
                            }
                        }catch (Exception e){}
                    }
                });
            }
        }).start();
        //异步更新数据1
        new Thread(new Runnable() {
            @Override
            public void run() {
                mContext.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (detailBean.getFifuploadimg() == 1) {
                                rvfEvalPics.setVisibility(VISIBLE);
                                if (detailBean.getAppraiseimgs().size() > 0) {
                                    final ArrayList<String> evalPics = new ArrayList<>();
                                    for(int i=0; i<detailBean.getAppraiseimgs().size(); i++){
                                        evalPics.add(detailBean.getAppraiseimgs().get(i).getFpath());
                                    }
                                    pManager = new FullyGridLayoutManager(mContext, 3);
                                    pAdapter = new PhotoAdapter(mContext, evalPics, PhotoAdapter.TYPE_URL, false);
                                    pAdapter.setListener(new PhotoAdapter.onItemClick() {
                                        @Override
                                        public void itemClick(View view, ArrayList<String> beans, int adapterPosition) {
                                            Bundle bundle1 = new Bundle();
                                            bundle1.putStringArrayList("piclists", evalPics);
                                            bundle1.putInt("position", adapterPosition);
                                            Intent intent = new Intent(mContext, ShowPicActivity.class);
                                            intent.putExtras(bundle1);
                                            mContext.startActivity(intent);
                                        }
                                        @Override
                                        public void deleteClick(View view, ArrayList<String> beans, int adapterPosition) {}
                                    });
                                    rvfEvalPics.setLayoutManager(pManager);
                                    rvfEvalPics.setAdapter(pAdapter);
                                }
                            } else {
                                rvfEvalPics.setVisibility(GONE);
                            }
                        }catch (Exception e){}
                    }
                });
            }
        }).start();
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerEvaluate.setLayoutManager(manager);
        if(detailBean.getFifstarlvappraise()==1) {//启用星级评价
            levlAdapter = new EvaluateLevlShowAdapter(mContext, detailBean.getAppraisestars());
        }else {//不启用星级评价
            levlAdapter = new EvaluateLevlShowAdapter(mContext, null);
        }
        mRecyclerEvaluate.setAdapter(levlAdapter);
    }
}

